﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Logic.Upload;
using PPI.Client.Presenter;
using PPI.Entities.XML;

namespace PPI
{
    public partial class frmUploadMobileDatabase : frmBase, IUploadMobileDatabase
    {
        protected EXmlOutboxBase m_OutboxBase;
        public EXmlOutboxBase OutboxBase
        {
            set { m_OutboxBase = value; }
        }
        private Stack<EXmlOutboxBase> m_SelectedOutboxesToUpload;
        private UploadMobileDatabasePresenter m_Presenter;

        public frmUploadMobileDatabase()
        {
            InitializeComponent();
            
            base.MenuMain.Visible = false;
            
            this.FormSize = FormSizes.Large;
            
            m_SelectedOutboxesToUpload = new Stack<EXmlOutboxBase>();

            m_Presenter = new UploadMobileDatabasePresenter(this);


        }

        private void frmUploadMobileDatabase_Load(object sender, EventArgs e)
        {
            this.outboxDatabase1.EntityXml = m_OutboxBase;
            this.outboxDatabase1.LoadOutboxesToUpload();
            this.outboxDatabase1.SetSelectionMultiple();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (outboxDatabase1.HasDatabases())
            {
                m_SelectedOutboxesToUpload = new Stack<EXmlOutboxBase>(outboxDatabase1.GetChekeckDatabases());
                if (this.StartUpload != null)
                    this.StartUpload(this, new EventArgs());
            }else
            {
                MessageBox.Show("Please selected a database to upload");
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (this.StopUpload != null)
                this.StopUpload(this, new EventArgs());

        }

        private void outboxDatabase1_ChangeSelection(object sender, EventArgs e)
        {
            m_Presenter.ShowDataInformation(outboxDatabase1.SelectedOutbox);
            m_Presenter.ShowImageInformation(outboxDatabase1.SelectedOutbox);
        }


        #region View

        public int ProgressBarValue
        {
            set { pbDataDownload.Value = value; }
        }

        public int ProgressBarValueImages
        {
            set { pbImageUpload.Value = value; }
        }

        public event EventHandler<EventArgs> StartUpload;
        public event EventHandler<EventArgs> StopUpload;

        public Stack<EXmlOutboxBase> SelectedOutboxesToUpload
        {
            get { return m_SelectedOutboxesToUpload; }
        }

        public bool StartStatus
        {
            set { btnStart.Enabled = value;               
                outboxDatabase1.Enabled = value;  
            }
        }

        public bool StopStatus
        {
            set { btnStop.Enabled = value; }
        }

        public int AmountData
        {
            set { lblnuAssets.Text = value.ToString(); }
        }

        public int AmountImage
        {
            set { lblnuImages.Text = value.ToString(); }
        }

        public string CompletedUploadedData
        {
            set { lblAssetStatusValue.Text = value; }
        }

        public string CompletedUploadedImage
        {
            set { lblImageStatusValue.Text = value; }
        }

        public string PathImage
        {
            set { lblImageDatabase.Text = value; }
        }

        public string PathData
        {
            set { lblDatabaseName.Text = value; }
        }

        public string MessageError
        {
            set { MessageBox.Show(value); }
        }

        public void reloadOutbox()
        {
            outboxDatabase1.LoadOutboxesToUpload();
        }

        public void ShowAlertCompleted()
        {
            if(this.Visible)
            {
                MessageBox.Show("Finish");
            }else
            {
                lblCompleted.Visible = true;
            }
        }

        #endregion

        private void btnHide_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        protected override void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            e.Cancel = true;
        }

        private void frmUploadMobileDatabase_VisibleChanged(object sender, EventArgs e)
        {
            if (outboxDatabase1.Enabled && this.Visible)
                reloadOutbox();
        }

        


    }
}
